package sketchml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Sketch {

	private Map ink;

	private int countOfInk;

	private Map shapes;

	private Map context;

	private int countOfContext;

	private void init() {
		countOfContext = 0;
		countOfInk = 0;

		ink = new HashMap();
		context = new HashMap();
		shapes = new HashMap();
	}

	public Sketch() {
		init();
	}

	public List getInk() {
		List list = new ArrayList();
		list.addAll(ink.values());
		return list;
	}

	public SMLStroke getStrokeById(int id) {
		return (SMLStroke) ink.get(new Integer(id));
	}

	public Object appendStroke(SMLStroke stroke) {
		Integer id = new Integer(countOfInk++);
		stroke.setId(id);
		return ink.put(id, stroke);
	}

	public Object removeStroke(SMLStroke stroke) {
		return removeStrokeById(stroke.getId());
	}

	public Object removeStrokeById(int id) {
		return ink.remove(new Integer(id));
	}

	public void appendStrokeAll(Collection strokes) {
		for (Iterator it = strokes.iterator(); it.hasNext();) {
			Object o = it.next();
			if (o instanceof SMLStroke) {
				SMLStroke stroke = (SMLStroke) o;
				appendStroke(stroke);
			} else {
				System.err.println("Sketch appendStrokes error: not a strokes");
			}
		}
	}

	public List getShapes() {
		List list = new ArrayList();
		list.addAll(shapes.values());
		return list;
	}

	public SMLShape getShapeByName(String name) {
		return (SMLShape) shapes.get(name);
	}

	public Object appendShape(SMLShape shape) {
		return shapes.put(shape.getName(), shape);
	}

	public Object removeShape(SMLShape shape) {
		return removeShapeByName(shape.getName());
	}

	public Object removeShapeByName(String name) {
		return shapes.remove(name);
	}

	public void appendShapeAll(Collection shapes) {
		for (Iterator it = shapes.iterator(); it.hasNext(); ) {
			Object o = it.next();
			if (o instanceof SMLShape) {
				SMLShape shape = (SMLShape) o;
				appendShape(shape);
			} else {
				System.err.println("Sketch appendShapes error: not a shape");
			}
		}
	}

	public List getContext() {
		List list = new ArrayList();
		list.addAll(context.values());
		return list;
	}

	public SMLCxtNode getCxtNodeById(int id) {
		return (SMLCxtNode) context.get(new Integer(id));
	}

	public Object appendCxtNode(SMLCxtNode cxtNode) {
		Integer id = new Integer(countOfContext++);
		cxtNode.setId(id);
		return context.put(id, cxtNode);
	}

	public Object removeCxtNode(SMLCxtNode cxtNode) {
		return removeCxtNodeById(cxtNode.getId());
	}

	public Object removeCxtNodeById(int id) {
		return context.remove(new Integer(id));
	}

	public void appendCxtNodeAll(Collection cxtNodes) {
		for (Iterator it = cxtNodes.iterator(); it.hasNext(); ) {
			Object o = it.next();
			if (o instanceof SMLCxtNode) {
				SMLCxtNode cxtNode = (SMLCxtNode) o;
				appendCxtNode(cxtNode);
			}
		}
	}
}
